/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

import java.io.*;
import java.net.*;

import nfse.*;

public class QuerySocket extends Thread {
    private Socket sock;

    private QueryServer queryServer;

    public QuerySocket(Socket s, QueryServer queryServer) {
        sock = s;
        this.queryServer = queryServer;
    }

    public void run() {
        try {
            sock.setKeepAlive(true);
            DataOutputStream out = new DataOutputStream(sock.getOutputStream());
            QueryHandler handler = new QueryHandler(this.queryServer);
            handler.parse(sock.getInputStream());
            Query query = handler.getQuery();
            System.out.println(query);
            if (query == null) {
                out.writeInt(-1); // Error code -1: query parse error
            } else {
                MetaDatabase mdb = null;
                try {
                    mdb = MetaDatabase.create();
                } catch (Exception mdbe) {
                    mdbe.printStackTrace();
                    mdb = null;
                }
                QueryProcessor qp = new QueryProcessor(this.queryServer, mdb);
                int userID = qp.authenticate(query);
                if (userID < 0) {
                    System.err.println("Could not authenticate user '" + query.getUser() + "'.");
                    System.out.println("Faulty query: " + query.toXML());
                    out.writeInt(-3); // Error code -3: user authentication
                    // error, save these in a log?!?
                    out.close();
                    sock.close();
                    return;
                }

                int val = query.validate();
                if (val == Query.QUERY_INVALID) {
                    System.err.println("Attempted to process an invalid query: '" + query.toXML() + "'");
                    out.writeInt(-4); // Error code -4: query could not be
                    // validated
                    out.close();
                    sock.close();
                    return;
                }
                if (val == Query.QUERY_EMPTY) {
                    System.out.println("Processed an empty query: '" + query.toXML() + "'");
                    out.writeInt(-5); // Error code -5: Empty query
                    out.close();
                    sock.close();
                    return;
                }
              
                out.writeInt(0);
                queryServer.addQuery(query);
                val = qp.process(query);
                mdb.closeConnection();
                queryServer.removeQuery(query.getID());
             
            }
            queryServer.endQuery();
            
            out.close();
            sock.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}